package com.kasax.imgloader.v1.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.FutureTask;

public class NetworkDispatcher {

    private Handler handler = new Handler(Looper.getMainLooper());

    private static volatile NetworkDispatcher dispatcher;

    private NetworkDispatcher() {

    }

    public static NetworkDispatcher getInstance() {
        if (dispatcher == null) {
            synchronized (NetworkDispatcher.class) {
                if (dispatcher == null) {
                    dispatcher = new NetworkDispatcher();
                }
            }
        }
        return dispatcher;
    }


    public static interface Callback {
        //        void onSuccess(InputStream inputStream) throws IOException;
        void onSuccess(Bitmap bitmap);

        void onFailure(Exception exception);
    }

    private class ImagLoadTask implements Runnable {

        private final String url;
        private final Callback callback;

        public ImagLoadTask(String url, Callback callback) {
            this.url = url;
            this.callback = callback;
        }

        @Override
        public void run() {
            try {
                HttpURLConnection httpURLConnection = configUrlConnection(url);
                dispatchConnection(httpURLConnection, callback);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void loadImage(String url, Callback callback) {
        ThreadExecutor.getInstance().enqueue(new FutureTask<>(new ImagLoadTask(url, callback), null));
    }

    private void dispatchConnection(HttpURLConnection httpURLConnection, Callback callback) throws IOException {
        InputStream inputStream = null;
        try {
            httpURLConnection.connect();
            int code = httpURLConnection.getResponseCode();
            String responseMessage = httpURLConnection.getResponseMessage();
            LogUtils.i(NetworkDispatcher.class, "code: " + code + " , message: " + responseMessage);
            if (code == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
                if (inputStream != null && callback != null) {
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onSuccess(bitmap);
                        }
                    });

                }
            } else {
                if (callback != null) {
                    callback.onFailure(new IOException(responseMessage));
                }
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }

        }
    }


    public HttpURLConnection configUrlConnection(String url) throws IOException {
        URL urlObj = new URL(url);
        HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
        urlConnection.setConnectTimeout(30 * 1000);
        urlConnection.setReadTimeout(30 * 1000);
        urlConnection.setDoInput(true);
        return urlConnection;

    }
}
